package com.friend.joey.applicationfriend.OldAcitivities;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.friend.joey.applicationfriend.Classes.TimeDeal;
import com.friend.joey.applicationfriend.R;

/**
 * Created by Joey on 2017/5/24.
 */

public class TimePupupWindow {

    public static final int DATEPOP=0;
    public static final int ALERTPOP=1;
    public static final int STATEIN=0;
    public static final int STATEOUT=1;
    public static final int STATEFINISHIN=2;
    public static final int STATEFINISHOUT=3;
    public static final int STATEFAIL=4;
    private PopupWindow popWnd;
    private View contentView;
    private View rootView;
    private TimeDeal timedeal;
    private int hour;
    private int type;
    private boolean finish=false;
    private int location;
    private ClickBack onClickBack;
    private ImageView imageView;
    private TextView textView;
    private DatePicker datePicker;
    private TimePicker timePicker;
    private Button backbutton;
    private Button yesbutton;

    public interface ClickBack{
        void onYesClick(TimeDeal time);
        void onNoClick();
    }

    public void setOnPopupWindowListener(ClickBack clickBack){
        this.onClickBack=clickBack;
    }


    public TimePupupWindow(LayoutInflater inflater,View rootView, int poptype){
        type=poptype;
        if(poptype==DATEPOP) {
            contentView = inflater.inflate(R.layout.popuplayout, null);
            popWnd = new PopupWindow(contentView);
            popWnd.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            popWnd.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            yesbutton = (Button) contentView.findViewById(R.id.YesWindowChoose);
            backbutton=(Button) contentView.findViewById(R.id.BackWindowChoose);
            timePicker = (TimePicker) contentView.findViewById(R.id.MyTimePicker2);
            datePicker = (DatePicker) contentView.findViewById(R.id.MyDatePicker2);
            yesbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    timedeal = new TimeDeal(datePicker.getYear(),datePicker.getMonth()+1,datePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute());
                    popWnd.dismiss();
                    //获取当前时间
                    if (onClickBack != null) {
                        onClickBack.onYesClick(new TimeDeal(datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute()));
                    }
                }
            });
            backbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popWnd.dismiss();
                    if (onClickBack != null) {
                        onClickBack.onNoClick();
                    }
                }
            });
            popWnd.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
        }
        else if(poptype==ALERTPOP){
            contentView = inflater.inflate(R.layout.popupalert,null);
            popWnd= new PopupWindow(contentView);
            popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            textView=(TextView)contentView.findViewById(R.id.alertText);
            changeState(STATEIN);
            popWnd.showAtLocation(rootView, Gravity.CENTER,0,0);
        }
    }

    public TimePupupWindow(LayoutInflater inflater,View rootView, TimeDeal time){
        type=DATEPOP;
        contentView = inflater.inflate(R.layout.popuplayout, null);
        popWnd = new PopupWindow(contentView);
        popWnd.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popWnd.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        yesbutton = (Button) contentView.findViewById(R.id.YesWindowChoose);
        backbutton=(Button) contentView.findViewById(R.id.BackWindowChoose);
        timePicker = (TimePicker) contentView.findViewById(R.id.MyTimePicker2);
        datePicker = (DatePicker) contentView.findViewById(R.id.MyDatePicker2);
        yesbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    timedeal = new TimeDeal(datePicker.getYear(),datePicker.getMonth()+1,datePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute());
                    popWnd.dismiss();
                    //获取当前时间
                    if (onClickBack != null) {
                        onClickBack.onYesClick(new TimeDeal(datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute()));
                    }
                }
            });
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWnd.dismiss();
                if (onClickBack != null) {
                    onClickBack.onNoClick();
                }
            }
        });
//        datePicker.init(time.getYear(), time.getMonth(), time.getDay(), new DatePicker.OnDateChangedListener() {
//            @Override
//            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
//
//            }
//        });
        datePicker.init(time.getYear(), time.getMonth()-1, time.getDay(), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {

            }
        });
        popWnd.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }

    public void changeState(int Type){
        if(type==ALERTPOP){
           switch(Type){
               case STATEIN:
                   if(!finish){
                       textView.setText("识别未完成，滑动或松开手指，取消识别");
                       break;
                   }
               case STATEFINISHIN:
                   finish=true;
                   textView.setText("识别完成，松开手指，确认发送，滑动手指，取消发送");
                   break;
               case STATEOUT:
                   if(!finish){
                       textView.setText("松开手指，取消识别");
                       break;}
               case STATEFINISHOUT:
                    finish=true;
                    textView.setText("识别完成，松开手指，取消发送");
                    break;
               case STATEFAIL:
                    textView.setText("识别失败，请松开手指，再次尝试");
                    type=-1;
                    break;
            }
        }
    }
    public void closePopupWindow(){
        popWnd.dismiss();
    }

    public void showPopupWindow(LayoutInflater inflater,View rootView) {
        contentView = inflater.inflate(R.layout.popuplayout,null);
        popWnd= new PopupWindow(contentView);
        //popWnd.setContentView();
        popWnd.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popWnd.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        Button button = (Button) contentView.findViewById(R.id.BackWindowChoose);
        final TimePicker timePicker=(TimePicker) contentView.findViewById(R.id.MyTimePicker2);
        DatePicker datePicker=(DatePicker) contentView.findViewById(R.id.MyDatePicker2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hour=0;
                hour=timePicker.getCurrentHour();
                popWnd.dismiss();
                //获取当前时间
                timedeal = new TimeDeal(TimeDeal.INITDATE);
            }
        });
        popWnd.showAtLocation(rootView, Gravity.BOTTOM,0,0);
    }
    public TimeDeal getTime(){
        return timedeal;
    }

    public int getHour(){
        return hour;
    }
}
